<?php

namespace App\Http\Controllers\Manage;

use App\Http\Model\Manage\AdminDepartmentModel;
use Illuminate\Contracts\View\View;
use Illuminate\Http\Request;

class DepartmentController extends BasicController
{

    /**
     * 删除部门科室
     * @param Request $request
     * @param string $code
     * @return array|string
     */
    public function deleteInfoAction(Request $request, string $code): array|string
    {
        $has = AdminDepartmentModel::where([
            ['code', '=', $code],
        ])->first();
        if (!$has)
        {
            return errorReturn("部门科室不存在");
        }
        $loginInfo = $this->getLoginInfo($request);
        $admin = $loginInfo['admin'];
        $role = $loginInfo['role'];
        $underDepartment = $this->getAllDepartmentUnderCode($admin->ltadCode);
        if (!in_array($has->code,$underDepartment))
        {
            return errorReturn("无权限操作该部门科室");
        }
        $underThisDepartment = $this->getAllDepartmentUnderCode($has->code);
        $delete = (new AdminDepartmentModel)->deleteInfo([], [
            "uLtaCode"=>$admin->code,
            "uLtarCode"=>$role->code,
        ], "code", $underThisDepartment);
        if ($delete)
        {
            return successReturn("操作成功");
        }
        return errorReturn("操作失败");
    }

    /**
     * 修改部门科室状态
     * @param Request $request
     * @param string $code
     * @return array|string
     */
    public function statusInfoAction(Request $request, string $code): array|string
    {
        $has = AdminDepartmentModel::where([
            ['code', '=', $code],
        ])->first();
        if (!$has)
        {
            return errorReturn("部门科室不存在");
        }
        $loginInfo = $this->getLoginInfo($request);
        $admin = $loginInfo['admin'];
        $role = $loginInfo['role'];
        $underDepartment = $this->getAllDepartmentUnderCode($admin->ltadCode);
        if (!in_array($has->code,$underDepartment))
        {
            return errorReturn("无权限操作该部门科室");
        }
        $update = (new AdminDepartmentModel)->updateInfo(dataToWhere([
            "code"=>$code,
            "version"=>$has->version
        ]), [
            "status"=>$has->status == 1 ? 0: 1,
            "uLtaCode"=>$admin->code,
            "uLtarCode"=>$role->code,
        ]);
        if ($update)
        {
            return successReturn("操作成功");
        }
        return errorReturn("操作失败");
    }

    /**
     * 添加部门科室
     * @param Request $request
     * @return View
     */
    public function addInfoAction(Request $request): array|string
    {
        $data = $request->all();
        $loginInfo = $this->getLoginInfo($request);
        $admin = $loginInfo['admin'];
        $role = $loginInfo['role'];
        $ltad001 = $data["ltad001"];
        $underDepartment = $this->getAllDepartmentUnderCode($admin->ltadCode);
        if (!in_array($data["pCode"], $underDepartment))
        {
            return errorReturn("无此上级科室");
        }
        $has = AdminDepartmentModel::where([
            ['ltad001', '=', $ltad001],
        ])->first();
        if ($has)
        {
            return errorReturn("科室已存在");
        }
        $adminId = (new AdminDepartmentModel)->createInfo([
            "ltad001"=>$ltad001,
            "ltad002"=>$data["ltad002"],
            "ltaCode"=>"",
            "pCode"=>$data["pCode"],
            "cLtaCode"=>$admin->code,
            "uLtaCode"=>$admin->code,
            "uLtarCode"=>$role->code,
        ]);
        if ($adminId > 0 )
        {
            return successReturn("添加成功");
        }
        else
        {
            return errorReturn("添加失败");
        }

    }

    /**
     * 修改部门科室信息
     * @param Request $request
     * @return View
     */
    public function editInfoAction(Request $request): array|string
    {
        $data = $request->all();
        $has = AdminDepartmentModel::where([
            ['code', '=', $data["code"]],
        ])->first();
        if (!$has)
        {
            return errorReturn("部门科室不存在");
        }
        $loginInfo = $this->getLoginInfo($request);
        $admin = $loginInfo['admin'];
        $role = $loginInfo['role'];
        $underDepartment = $this->getAllDepartmentUnderCode($admin->ltadCode);
        if (!in_array($has->code,$underDepartment))
        {
            return errorReturn("无权限操作该部门科室");
        }
        $underThisDepartment = $this->getAllDepartmentUnderCode($has->code);
        if (in_array($data["pCode"],$underThisDepartment))
        {
            return errorReturn("不可以将上级部门科室设置为其下级部门科室");
        }
        $update = (new AdminDepartmentModel)->updateInfo(dataToWhere([
            "code"=>$has->code,
            "version"=>$has->version,
        ]), [
            "lta004"=>$data["lta004"],
            "lta005"=>$data["lta005"],
            "pCode"=>$data["pCode"],
            "uLtaCode"=>$admin->code,
            "uLtarCode"=>$role->code,
        ]);
        if ($update)
        {
            return successReturn("操作成功");
        }
        return errorReturn("操作失败");
    }

    /**
     * 部门科室列表页面
     * @param Request $request
     * @return View
     */
    public function pageTableView(Request $request): View
    {
        $loginInfo = $this->getLoginInfo($request);
        $admin = $loginInfo['admin'];
        $underDepartment = $this->getAllDepartmentUnderCode($admin->ltadCode);
        $underDepartment = AdminDepartmentModel::whereIn("code", $underDepartment)->where([
            [ 'deleted', "=", 0],
        ])->orderBy('id','asc')->get()->toarray();
        return view($this->viewPath."system.department_table",  ["departmentList"=>$underDepartment]);
    }

    /**
     * 部门科室列表数据
     * @param Request $request 请求参数 page 当前页码 limit 每页数量
     * @return array|string
     */
    public function pageTableData(Request $request): array|string
    {
        $loginInfo = $this->getLoginInfo($request);
        $admin = $loginInfo["admin"];
        $underDepartment = $this->getAllDepartmentUnderCode($admin->ltadCode, true);
        $underDepartment = AdminDepartmentModel::whereIn("code", $underDepartment)->where([
            [ 'deleted', "=", 0],
        ])->orderBy('id','asc')->get()->toarray();
        $treeData = dataToTree($underDepartment, "code", "pCode");
        return successReturn("加载成功", $treeData);
    }

}
